<?php

namespace app\product\logic;
use app\common\library\Excel;
use app\common\library\RedissLock;
use app\common\service\CommonSystemImportService;
use app\data\util\MqQueueName;
use app\systems\logic\MqTaskPushLogic;
use think\facade\Db;
class ProductOnwayLogic extends BasicLogic
{



    /**
     * @desc   导入产品库存
     * @author luckyXiong
     * @date   2024-07-24
     */
    public function import_inventor_count($params){
        try{
            $result = CommonSystemImportService::staticModel()->operate_import_data([
                    $this,//当前对应的service
                    'save',//类型: template 下载模板; save :保存 导入
                    CommonSystemImportService::TYPE_IMPORT_ONWAY_PAGE,
                    1,
                    $params
                ]
            );
        }catch (\Exception $e) {
            return $this->jsonMessage(201, false, $e->getMessage(), null);
        }
    }



    /**
     * @desc   修改库存
     * @author luckyXiong
     * @date   2024-08-01
     */
    public function change_inventory(){
            $where   = [];
            $where[] = ['spu','<>',''];
            Db::name('product_inventory')->where($where)->update([
               'buy_inventory_quantity'   =>0,
               'arrive_inventory_quantity'=>0,
             ]
           );

          Db::name('product_spu_inventory')->where($where)->update([
                'purchase_number_num'=>0,
            ]
          );

          //update 2024-08-14  同时清空qu_product_buy_inventory中数据，获取最新的;
          Db::execute('TRUNCATE TABLE qu_product_buy_inventory');
    }



    /**
     * @desc 将导入数据写入目标表
     * @author xx
     * @date 2024-03-21
     */
    public function import_inventory_onway($params=[]){
        $key                      = 'import_inventory_onway:'.time();
        $redis_lock               = new RedissLock();
        $lock                     = $redis_lock::lock($key, $redis_lock::UNBLOCK, 30);
        $seller_sku               = getIsset($params,'seller_sku');
        $product_name             = getIsset($params,'product_name');
        $purchase_number          = getIsset($params,'purchase_number');
        $purchase_time            = getIsset($params,'purchase_time');
        $buy_inventory            = $params['buy_inventory']??'0';
        $arrive_inventory         = $params['arrive_inventory']??'0';
        $warehouse                = getIsset($params,'warehouse');
        $seller_sku_arr           = explode('-',$seller_sku);
        if(!$buy_inventory){
            $buy_inventory    = 0;
        }
        if(!$arrive_inventory){
            $arrive_inventory = 0;
        }
        $on_way_quantity          = $buy_inventory > 0 ? $buy_inventory-$arrive_inventory : 0;
        $spu                      = $seller_sku_arr[0]??'';
        //查看数据是否存在;
        $exist_info   = Db::name('product_inventory')->where('seller_sku',$seller_sku)->find();
        if($exist_info){
            Db::name('product_inventory')->where('id',$exist_info['id'])->update(
                [
                    'buy_inventory_quantity'    =>$buy_inventory,
                    'arrive_inventory_quantity' =>$arrive_inventory,
                ]
            );
        }

        //采购单号处理;
        $temp_purchase = [
            'spu'                =>$spu,
            'seller_sku'         =>$seller_sku,
            'purchase_number'    =>$purchase_number,
            'create_time'        =>date("Y-m-d H:i:s", time()),
        ];
        if($purchase_time) $temp_purchase['purchase_time'] = $purchase_time;
        Db::name('product_buy_inventory')->insert($temp_purchase);
        $lock->unlock();
        $info['status'] = 1;
        return  $info;
    }


    /**
     * @param   $row
     * @return  string[]
     * @desc   数据title 导入组装数据结构
     */
    public function get_excel_row_data($row){

        return [
            'seller_sku'             => empty($row['F'])? '': trim($row['F']),
            'buy_inventory'          => empty($row['I'])? '': trim($row['I']),
            'arrive_inventory'       => empty($row['J'])? '': trim($row['J']),
            'warehouse'              => empty($row['R'])? '': trim($row['R']),
            'product_name'           => empty($row['C'])? '': trim($row['C']),
            'track_number'           => empty($row['Z'])? '': trim($row['Z']),
            'purchase_number'        => empty($row['A'])? '': trim($row['A']),
            'purchase_time'          => empty($row['AC'])? '': trim($row['AC']),
        ];
    }
}